﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Linq;

namespace Dsl.Ped.Common.Helpers {

    /// <summary>
    /// Helper for the <see cref="Type"/> of Enum
    /// </summary>
    public class EnumHelper {

        /// <summary>
        /// Localize the Enumerator against the Lang dictionary of specific resource and <see cref="Assembly"/>
        /// </summary>
        /// <typeparam name="T">Type of the enum</typeparam>
        /// <param name="enum">Enum value</param>
        /// <param name="resourceAssembly"><see cref="Assembly"/> where the resource is defined</param>
        /// <param name="resourceManagerBaseName">Base name of the resource</param>
        /// <returns>Localized dictionary item if exists, enum.value to string otherwise</returns>
        public static string Localize<T>(T @enum, Assembly resourceAssembly, string resourceManagerBaseName) {

            var rm = new ResourceManager(resourceManagerBaseName, resourceAssembly);

            var name = string.Format("Enum{0}_{1}", @enum.GetType().Name, @enum);
            return rm.GetString(name) ?? @enum.ToString();
        }

        /// <summary>
        /// Get a List of all enum values of a specific enum <see cref="Type"/>
        /// </summary>
        /// <typeparam name="T"><see cref="Type"/> of enum</typeparam>
        /// <returns>Colleciton of all enum values</returns>
        public static List<T> GetEnums<T>()
            where T : struct
        {
            if (!typeof(T).IsEnum)
            {
                throw new Exception(string.Format("Type '{0}' is not Enum", typeof(T)));
            }

            var enumsArray = Enum.GetValues(typeof(T));
            return enumsArray.OfType<T>().ToList();
        }
    }
}
